#include <cstdlib>
#include <iostream>
#include <conio.h>
#include <stdlib.h>

//OPENAL (use for 3D sound effect ingame)
#include <al/al.h>
#include <al/alc.h>
#include <al/alut.h>
#include <vector.h>

//DML (use functions: bitmap)
#include <dml/dml.h>
#include <extensions/gldml.h>

//Projectfiles
#include "bullet.h"
#include "camera.h"
#include "engine.h"
#include "player.h"
#include "world.h"
#include "resource.h"

//Definities van het scherm en aantal kogels.
#define width 1024
#define height 768
#define MAXBULLETS 200

//objecten.
camera Camera;
player Player;
world  World;
bullet Bullet[MAXBULLETS];

void DML_main()
{
    //BEGIN INIT
    DML_InitOpenGL(width,height,"opengl");
    DML_settitle("Opengl Game by barth steeg");
    InitOpenAL();
    
    //Inladen bestanden INIT
    corefile_mountimage("res.dat",MOUNT_FILE);
    init_resource();
    
    //INIT objecten
    Camera.init(width,height,45,0.0f,0.0f,0.0f, 1.0f,6.0f,-2.0f);
    Player.init(Camera.View.x, (Camera.View.y+30.0f), (Camera.View.z+19.0f),3,3.0f,0.1f);
    World.init((Camera.View.x-1.0f), (Camera.View.y+3.0f), (Camera.View.z+7.5f),0.5f);
    
    //INIT light opengl
    GLfloat LightAmbient[]=  { 0.1f, 0.0f, 0.2f, 1.0f };  
    GLfloat LightDiffuse[]=  { 1.0f, 1.0f, 1.0f, 1.0f };  
    GLfloat LightPosition[]= { Camera.View.x, Camera.View.y, Camera.View.z, 2.0f };  
    
    glEnable(GL_TEXTURE_2D);                            // Enable Texture Mapping
    glEnable(GL_NORMALIZE);
    glShadeModel(GL_SMOOTH);                            // Enable Smooth Shading
    glClearColor(0.0f, 0.0f, 0.0f, 0.5f);               // Black Background
    glClearDepth(1.0f);                                 // Depth Buffer Setup
    glEnable(GL_DEPTH_TEST);                            // Enables Depth Testing
    glDepthFunc(GL_LEQUAL);                             // The Type Of Depth Testing To Do
    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);  // Really Nice Perspective Calculations

    glLightfv(GL_LIGHT1, GL_AMBIENT, LightAmbient);     // Setup The Ambient Light
    glLightfv(GL_LIGHT1, GL_DIFFUSE, LightDiffuse);     // Setup The Diffuse Light
    glLightfv(GL_LIGHT1, GL_POSITION,LightPosition);    // Position The Light    
    
    glEnable(GL_LIGHT1);
    glEnable(GL_LIGHTING);
    
    GLfloat LightDiffuse2[]=  { 1.0f, 1.0f, 1.0f, 1.0f };
    GLfloat LightPosition2[]= { Camera.View.x, (Camera.View.y+24.1f), (Camera.View.z+25.5f), 1.0f };
    glLightfv(GL_LIGHT2, GL_DIFFUSE, LightDiffuse2);    // Setup The Diffuse Light
    glLightfv(GL_LIGHT2, GL_POSITION,LightPosition2);   // Position The Light
    glDisable(GL_LIGHT2);
    
    //INIT sound openal
    ALfloat ListenerPos[] = { 0.0, 0.0, 0.0 };      // Position of the Listener.
    ALfloat ListenerVel[] = { 0.0, 0.0, 0.0 };      // Velocity of the Listener.

    // Orientation of the Listener. (first 3 elements are "at", second 3 are "up")
    // Also note that these should be units of '1'.
    ALfloat ListenerOri[] = { 0.0, 0.0, -1.0,  0.0, 1.0, 0.0 };
    
    alListenerfv(AL_POSITION,    ListenerPos);
	alListenerfv(AL_VELOCITY,    ListenerVel);
	alListenerfv(AL_ORIENTATION, ListenerOri);
    
    //GLOBAL variabelen INIT
    int timer;
    timer = 0;
    bool shoot;
    shoot = false;
    
    //INIT bullets
    for(int i=0; i<MAXBULLETS; i++)
    {
        Bullet[i].active = false;
    }
    //END INIT
    
    do
    {
        //  view & camera
        Camera.clear();
        Camera.look();
        
        //  begin checks !!
        World.update(Player.get_playery());
        
        if(Player.update()==1)
        {
            if(!shoot)
            {
                shoot = true;
                glEnable(GL_LIGHT2);
                
                for(int i=0; i<MAXBULLETS; i++)
                {
                    if(!Bullet[i].active) 
                    {
                        PlaySound(&effect_laser);
                        
                        //GLfloat LightPosition3[]= { Player.get_playerx(), Player.get_playery(), (Player.get_playerz()+4.0f), 1.0f };
                        //glLightfv(GL_LIGHT2, GL_POSITION, LightPosition3);
                        
                        Bullet[i].init(Player.get_playerx(), Player.get_playery(), Player.get_playerz(), 300, 5.0f);
                        break;
                    }
                }
            }
        }
        
        if(shoot)
        {
            timer++;
        }
        
        if(timer > 100)
        {
            glDisable(GL_LIGHT2);
            timer = 0;
            shoot = false;
        }
        
        for(int i=0; i<MAXBULLETS; i++)
        {
            Bullet[i].update();
        }
        //  end checks !!


        //  begin draw !!
        for(int i=0; i<MAXBULLETS; i++)
        {
            Bullet[i].draw();
        }
        
        Player.draw(); 
        World.draw();
        
        
        //swap now
        SwapBuffers(hdc);
    }while (1);
}
